﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Royaumes_Libres.Database;

namespace Royaumes_Libres
{
    public partial class kingdoms : System.Web.UI.Page
    {
        cUsers pUser;
        cWorld[] myWorlds;
        cWorld[] allWorlds;    

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Properties.Settings.Default.ObjUser] != null)
            {
                pUser = (cUsers)Session[Properties.Settings.Default.ObjUser];
                myWorlds = cWorld.GetUserWorld(pUser.PkUser);
                GetWorlds();
            }
            else
            {
                ViewWorlds();
            }
        }

        protected void GetWorlds() {

            allWorlds = cWorld.GetAllWorlds();

            this.divMyKingdoms.InnerHtml = "<h1>" + cText.GetText("MyKingdom") + "</h1>";
            this.divNewKingdoms.InnerHtml = "<h1>" + cText.GetText("NewKingdom") + "</h1>";

            int im = 0;
            int i = 0;
            while (i < allWorlds.Count())
            {
                if (im < myWorlds.Count())
                {
                    if (myWorlds[im].PkWorld == allWorlds[i].PkWorld)
                    {
                        ucWorlds uc = myWorlds[im].GetControl(Page);
                        uc.world = myWorlds[im];
                        uc.isNew = false;
                        this.divMyKingdoms.Controls.Add(uc);
                        im++;
                    }
                    else
                    {
                        ucWorlds uc = allWorlds[i].GetControl(Page);
                        uc.world = allWorlds[i];
                        uc.isNew = true;
                        this.divNewKingdoms.Controls.Add(uc);
                    }
                }
                else
                {
                    ucWorlds uc = allWorlds[i].GetControl(Page);
                    uc.world = allWorlds[i];
                    uc.isNew = true;
                    this.divNewKingdoms.Controls.Add(uc);
                }
                ++i;
            }

            if (myWorlds.Count() == 0)
            {
                this.divMyKingdoms.InnerHtml = "<p>" + cText.GetText("ErrorNoKingdom") + "</p>";
            }
            else
            {
                if (myWorlds.Count() == allWorlds.Count())
                {
                    this.divNewKingdoms.Visible = false;
                }
            }
        }

        protected void ViewWorlds()
        {
            allWorlds = cWorld.GetAllWorlds();

            this.divNewKingdoms.InnerHtml = "<h1>" + cText.GetText("AllKingdom") + "</h1>";

            int i = 0;
            while (i < allWorlds.Count())
            {
                ucWorlds uc = allWorlds[i].GetControl(Page);
                uc.world = allWorlds[i];
                uc.isNew = true;
                uc.isGuest = true;
                this.divNewKingdoms.Controls.Add(uc);
                ++i;
            }

            this.divMyKingdoms.Visible = false;
        }

    }
}